home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10862 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: in2.uu.net!interaccess!usenet
  2. From: brianmcg@interaccess.com (Brian V. McGroarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Indexfiles
  5. Date: 20 Mar 1996 14:18:47 GMT
  6. Organization: Internet Squire
  7. Message-ID: <4ip447$g6h@nntp.interaccess.com>
  8. References: <735.6653T15T2452@mailbox.swipnet.se>
  9. Reply-To: brianmcg@interaccess.com
  10. NNTP-Posting-Host: d41-isdn.nhe.interaccess.com
  11. X-Newsreader: Internet Squire 1.20
  12.  
  13. Jonas Kjellin wrote:
  14. > How do i make a indexfile?
  15. >
  16. > I mean, so you can write, create, update, search, delete recordes...
  17.  
  18. I will assume these records are of varying sizes.  If they are not, you can
  19. go without the index file and simply perform an fseek to the location which
  20. is "n" times the size of your record to point directly at the "n"th record.
  21.  
  22.  
  23. > If i would like to find a record fast, is it better to make a special index
  24. > file who only have the positions for the records in the datafile or?
  25. >
  26. > I mean i would like it as a database.. byt how do i do it fast, so i dont
  27. > need to start from the beginning of the file and then read every record
  28. > and compare if it matches my keys..?
  29.  
  30. The structure of such a file would be that the index file is simply an
  31. array of offsets into the file at which each record is located.  To seek to
  32. the "n"th record you would seek to the "n"th entry in the index file, i.e 
  33.  
  34.     fseek( yourFile, sizeof( indexEntry )*n, SEEK_SET )
  35.  
  36. Here, you read the indexEntry data item (presumably an integer type) and
  37. seek that far into the main record file... this points you directly at the
  38. data record without having to do any key searching.  
  39.  
  40.  
  41.  
  42. > and how do i delete a record in the
  43. > middle of a big datafile? do i have to rewrite the whole file??
  44.  
  45. If you wish to be able to delete entries then you should consider keeping a
  46. free list of some sort.  One approach to this would be to keep a second
  47. file which is simply a listing of file offsets of and sizes of free blocks
  48. within the main database file.  Any time you remove an item from the
  49. database, you would run through the list of free entries to see if you are
  50. "next to" another entry.  If you are, you would then expand the existing
  51. entry to encompass the new entry.  If this is done, then you will also need
  52. to check to see if this entry now becomes adjacent another entry which
  53. would also need to be merged.  Now, any time you wish to create a new
  54. record, you would first check to see if there is an item in the free file
  55. which matches the size of the entry you wish to create.  If not, you would
  56. extend the length of the file as usual.
  57.  
  58.  
  59.  
  60.  
  61. ---
  62. Brian Valters McGroarty -- brianmcg@bix.com
  63. phone/fax (847) 439-7714
  64.  
  65.  
  66.  
  67.